package com.lcz.cloud_note.service.impl;

import com.lcz.cloud_note.dao.ActionDao;
import com.lcz.cloud_note.dao.ActionDetailDao;
import com.lcz.cloud_note.dao.NoteDao;
import com.lcz.cloud_note.entity.ActionDetail;
import com.lcz.cloud_note.entity.Note;
import com.lcz.cloud_note.service.IActionService;
import com.lcz.cloud_note.util.ActionUtils;
import com.lcz.cloud_note.util.NoteUtil;
import com.lcz.cloud_note.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ActionServiceImpl implements IActionService
{
    @Autowired
    private NoteDao noteDao;
    @Autowired
    private ActionDao actionDao;
    @Autowired
    private ActionDetailDao actionDetailDao;

    public Result listAction() {
        return Result.ok(actionDao.list());
    }

    public Result addAction(String actionId, String noteId)
    {
        //查询笔记
        Note note = noteDao.findByNoteId(noteId);
        //查询是否存在活动
        ActionDetail actionDetail = actionDetailDao.getByNoteId(noteId);
        if(actionDetail == null)
        {
            actionDetail = new ActionDetail();
            actionDetail.setCn_note_activity_id(NoteUtil.createId());
            actionDetail.setCn_activity_id(actionId);
            actionDetail.setCn_note_id(noteId);
            actionDetail.setCn_note_activity_title(note.getCn_note_title());
            actionDetail.setCn_note_activity_body(note.getCn_note_body());
            actionDetailDao.insert(actionDetail);
            return Result.ok(actionDetail);
        }else
            {//判断活动笔记是否有更新 有更新就更新
                if(actionDetail.getCn_note_activity_title().equalsIgnoreCase(note.getCn_note_title())
                    && actionDetail.getCn_note_activity_body().equalsIgnoreCase(note.getCn_note_body())
                )
                {
                    return Result.fail("该笔记已参与活动");
                }else
                    {
                        return Result.ok(actionDetail);
                    }
            }
    }

    public Result listActionDetail(String actionId, int page)
    {
        int begin = (page-1) * 15;
        int end = begin+14;
        List<ActionDetail> actionDetails = actionDetailDao.listByActionId(actionId,begin,end);
        return Result.ok(actionDetails);
    }

    public Result showActionDetail(String actionDetailId)
    {
        return Result.ok(actionDetailDao.getById(actionDetailId));
    }

    public Result downActionDetail(String userId, String actionDetailId)
    {
        //判断是否能踩(24小时内不允许重复踩)
        if(!ActionUtils.downAction(userId,actionDetailId))
        {
            return Result.fail("今天您已经踩过啦~!");
        }
        ActionDetail actionDetail = actionDetailDao.getById(actionDetailId);
        actionDetail.setCn_note_activity_down(actionDetail.getCn_note_activity_down()+1);
        actionDetailDao.dynamicUpdate(actionDetail);
        return Result.ok("点踩成功!");
    }

    public Result upActionDetail(String userId, String actionDetailId)
    {
        if(!ActionUtils.upAction(userId,actionDetailId))
        {
            return Result.fail("今天您已经赞过啦~!");
        }
        ActionDetail actionDetail = actionDetailDao.getById(actionDetailId);
        actionDetail.setCn_note_activity_up(actionDetail.getCn_note_activity_up()+1);
        actionDetailDao.dynamicUpdate(actionDetail);
        return Result.ok("点赞成功!");
    }

    public Result actionDetailByUser(String userId)
    {
        List<ActionDetail> actionDetails = actionDetailDao.listByUserId(userId);

        return Result.ok(actionDetails);
    }

    public Result showActionDetailByNoteId(String noteId) {
        return Result.ok(actionDetailDao.findByNoteId(noteId));
    }
}
